/**
 * Created by xihashao-pc on 2017/1/17.
 */

/** 获取保修问题，根据parentId*/
function getQuestion(parentId,$this){
    $($this).html("<option></option>");
    $.getJSON("http://localhost:8080/JGX/RepairOrder_queryRepairQuestion.json",{parentId : parentId},function(data){
        console.log(data);
        if(data.success && data.repairQuestions != null){
            $.each(data.repairQuestions,function(index,item){
                $($this).append("<option  value="+item.id+" id="+item.id+">"+item.questionDescrible+"</option>");
            });
        }
    });
}
/** 获取楼盘地址*/
function getAddress(parentId,$this){
    $($this).html("<option></option>");
    $.getJSON("http://localhost:8080/JGX/houseAndAddress_queryHouseAddress.json",{parentId : parentId},function(data){
        console.log(data);
        if(data.success && data.buildingInformations != null){
            $.each(data.buildingInformations,function(index,item){
                $($this).append("<option  value="+item.id+" id="+item.id+">"+item.buildingName+"</option>");
            });
        }
    });
}

function addRepaireOrder2(){
var fd = new FormData($('#repairOrderForm')[0]);
console.log(arr)
for (var i = 0, j = arr.length; i < j; ++i) {
fd.append('myFileTest[]', arr[i]);
}
    console.log(typeof fd)
    $.ajax({
        type:"POST",
        url:'http://xihashao.tunnel.2bdata.com/JGX/RepairOrder_addRepairOrder.json',
        data:fd,
        processData: false,
        contentType: false,
        success:function(){
            console.log(123)
        },
        error:function(){
            console.log("error")
        }
    })
}

/** 提交报修单，提交成功后跳转到我的保修单*/
function addRepairOrder(){
    var form = $("form[id='repairOrderForm']");
    var options  = {
        url:'http://localhost:8080/JGX/RepairOrder_addRepairOrder.json',
        type:'post',
        success:function(data)
        {
            if(data.success == true){
                $.alert(data.msg);
                // location.href();

            }else{
                $.alert(data.msg) ;
            }
        }
    };
    form.ajaxSubmit(options);
     
}

/**问题选择后出发子集问题的获取方法 */

/**获取用户的房产信息*/
function getHouseProperties(){
    $(".weui-check__label").remove();
    $.getJSON("http://localhost:8080/JGX/houseAndAddress_queryHouseProperty.json",function(data){
        console.log(data);
        if(data.success && data.housePropertys != null){
            $.each(data.housePropertys,function(index,item){
                console.log("--------");
                $("#housePropertiesList").append("<label class=\"weui-cell weui-check__label\" for=x"+item.id+">"
                    +"<div class=\"weui-cell__bd\">"
                    +"<p>"+item.developerName+item.buildingName+item.buildingStage+item.buildingNumber+item.houseNumber+"</p>"
                    +"</div>"
                    +"<div class=\"weui-cell__ft\">"
                    +"<input type=\"radio\" class=\"weui-check\" name=\"radio1\" id=x"+item.id+">"
                    +"<span class=\"weui-icon-checked\"></span>"
                    +"</div>"
                    +"</label>");
            });
        }
    });

}
/**提交新增我的房产信息*/
function addHouseProperties(){
    var data1=$("#houseAddress1").find("option:selected").text();
    var data2=$("#houseAddress2").find("option:selected").text();
    var data3=$("#houseAddress3").find("option:selected").text();
    var data4=$("#houseAddress4").find("option:selected").text();
    var data5=$("#houseAddress5").find("option:selected").text();
    if(data1==""||data2==""||data3==""||data4==""||data5==""){
        alert("请选择完整的房产信息");
        return false;
    }
    var hp=data1+","+data2+","+data3+","+data4+","+data5;

    var BuildingStageId=$("#houseAddress3").find("option:selected").attr("id");
    console.log(hp+"--"+BuildingStageId);
    $.getJSON("http://localhost:8080/JGX/houseAndAddress_addHouseProperty.json",{hp:hp,BuildingStageId:BuildingStageId},function(data){
        console.log(data);
        if(data.success){
            alert("添加成功！");
            $(".fourthPage").hide();
            $(".thirdPage").show();
            getHouseProperties(); //重新获取用户的房产信心

        }
        else{
            $.alert(data.msg);
        }
    });


}

function changePart() {
    location.hash = "part5";
    var x = "锚部分现在为: " + location.hash;
    // document.getElementById("demo").innerHTML = x;
}

// /**弹出正确提示toast */
// function showToast(content){
//     $("#toast").show();
//     if ($("#toast").css('display') != 'none')
//         return;function getHash(url) {

//
//     $("#toast").fadeIn(100);
//     setTimeout(function () {
//         $("#toast").fadeOut(100);
//     }, 2000);
//     $(".weui_toast_content").html(content);
// }
$(document).ready(function() {

    window.addEventListener("hashchange", function(e) {


        console.log("旧的哈希值："+e.old.hash);
        if(location.hash=="#secondPage"){
           return false;
        }
        else{
            $(".secondPage").hide();
            $(".firstPage").show();
        }

         

    }, false);

    // window.addEventListener("hashchange", myFunction);
    // function myFunction() {
    //     alert("锚部分已修改！");
    // }


    getQuestion(0,$("#proStep1"));
    /**问题选择后出发子集问题的获取方法 */
    $("#proStep1").change(function () {
        // console.log(this);
        getQuestion(this.value,$("#proStep2"));
    });
    $("#proStep2").change(function () {
        getQuestion(this.value,$("#proStep3"));
    });

    /**获取用户的房产信息*/
    getHouseProperties();

    /** 获取楼盘地址(select)*/
    getAddress(0,$("#houseAddress1"));

    $("#houseAddress1").change(function () {
        // console.log(this);
        getAddress(this.value,$("#houseAddress2"));
    });

    $("#houseAddress2").change(function () {
        // console.log(this);
        getAddress(this.value,$("#houseAddress3"));
    });
    $("#houseAddress3").change(function () {
        // console.log(this);
        getAddress(this.value,$("#houseAddress4"));
    });

    $("#houseAddress4").change(function () {
        // console.log(this);
        getAddress(this.value,$("#houseAddress5"));
    });


    /**提交新增我的房产信息*/
    $("#postHouseProperties").on("click",function(){
        addHouseProperties();

    })

    $("#submitRepair").on("click",function () {
        console.log("hopeDate:"+$("#repairOrderForm  input[name='hopeDate']").val());
        // if($("#repairOrderForm  input[name='hopeDate']").val()==""){

        //     $.alert('请选择期望上门时间！');

        // }
        // else if($("#repairOrderForm  input[name='questionOne']").val()==""||$("#repairOrderForm  input[name='questionTwo']").val()==""||$("#repairOrderForm  input[name='questionThree']").val()==""){
        //     $.alert('报修问题未选择完全！');
        // }
        // else if($("#repairOrderForm  input[name='files']").val()==undefined||$("#repairOrderForm  input[name='files']").val()==""){

        //     $.alert('请上传图片后再提交！');
        // }
        // else if($("#repairOrderForm  input[name='username']").val()==""){
        //     $.alert("请填写联系人姓名");
        // }

        // else if($("#repairOrderForm  input[name='userTel']").val()==""){
        //     $.alert("请填写联系人电话");
        // }
        // else if($("#repairOrderForm  input[name='userTel']").val().length!=11){
        //     $.alert("手机号码格式不正确");
        // }

        // else if($("#repairOrderForm  input[name='housePropertyId']").val()==""){
        //     $.alert("请选择报修地址");
        // }
        // else{
        //     addRepairOrder();
        // }
        addRepairOrder()
        // addRepaireOrder();

    })
})


